//
//  TranslucentViewController.m
//  Romens.XSBalance
//
//  Created by 王腾 on 16/2/26.
//  Copyright © 2016年 孙博. All rights reserved.
//

#import "TranslucentViewController.h"
#import "ToolHelper.h"
#import "ZVScrollSlider.h"
@interface TranslucentViewController ()
{
    NSArray * viewAllConstraintsOfVertical;//视图中VIEW所有控件的竖直方向的约束；
}
@end

@implementation TranslucentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    UIView *alphaView = [[UIView alloc] initWithFrame:self.view.frame];
    UIView *baseView = [[UIView alloc] initWithFrame:self.view.frame];
    alphaView.backgroundColor = [UIColor clearColor];
    baseView.backgroundColor = [UIColor lightGrayColor];
    baseView.alpha = 0.5;
    [self.view addSubview:baseView];
    [self.view addSubview:alphaView];
    //创建手势
    UITapGestureRecognizer* gesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ok)];
    [alphaView addGestureRecognizer:gesture];
    [self initview];
    self.IsShowUserInfo=false;
    
    __weak TranslucentViewController*tVC=self;
    self.closeKeyboardBlock=^()
    {
        [tVC Setkeyboard];
        [tVC.view endEditing:NO];
    };
    self.keyboardBlock=^(CGFloat keyHeight)
    {
        //        float sssss=keyHeight;
        //        float viewheight=220;
        //        float viewwidth=FULL_WIDTH;
        //        float viewX=0;
        //        float viewY=keyHeight-viewheight;
        if (keyHeight==224) {
            [tVC SetkeyboardBlock];
        }else if (keyHeight==440||keyHeight==480) {
            [tVC Setkeyboard];
        }
    };
}
-(void)initview{
    self.sexView=[[UIView alloc]init];
    self.sexView.translatesAutoresizingMaskIntoConstraints=NO;
    self.sexView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.sexView];
    UIView * titleView=[[UIView alloc]init];
    titleView.translatesAutoresizingMaskIntoConstraints=NO;
    titleView.backgroundColor=SystemColor;
    [self.sexView addSubview:titleView];
    
    self.sexLabel=[[UILabel alloc]init];
    self.sexLabel.translatesAutoresizingMaskIntoConstraints=NO;
    self.sexLabel.textAlignment = NSTextAlignmentCenter;
    self.sexLabel.textColor=[UIColor whiteColor];
    self.sexLabel.font=systemFont(22);
    [titleView addSubview:self.sexLabel];
    
    self.okBt=[[UIButton alloc]init];
    self.okBt.translatesAutoresizingMaskIntoConstraints=NO;
    [self.okBt addTarget:self action:@selector(ok) forControlEvents:UIControlEventTouchUpInside];
    self.okBt.imageView.contentMode=UIViewContentModeScaleAspectFit;
    [self.okBt setImage:[UIImage imageNamed:@"yes"] forState:UIControlStateNormal];
    [titleView addSubview:self.okBt];
    
    self.backBt=[[UIButton alloc]init];
    self.backBt.translatesAutoresizingMaskIntoConstraints=NO;
    [self.backBt addTarget:self action:@selector(ok) forControlEvents:UIControlEventTouchUpInside];
    self.backBt.imageView.contentMode=UIViewContentModeScaleAspectFit;
    [self.backBt setImage:[UIImage imageNamed:@"no"] forState:UIControlStateNormal]; [titleView addSubview:self.backBt];
     //int H= (FULL_HEIGHT)/2;
    [self.view addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_sexView) format:@"H:|[_sexView]|"]];
    viewAllConstraintsOfVertical=[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_sexView) format:@"V:[_sexView(190)]|"];
    [self.view addConstraints:viewAllConstraintsOfVertical];
//    viewAllConstraintsOfVertical=[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_sexView) format:[NSString stringWithFormat:@"V:|-(%d)-[_sexView]",H]];
//    [self.view addConstraints:viewAllConstraintsOfVertical];
    [self.sexView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(titleView) format:@"H:|[titleView]|"]];
    [self.sexView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(titleView) format:@"V:|[titleView(44)]"]];
    
    [titleView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_backBt,_sexLabel,_okBt) format:@"H:|[_backBt(44)][_sexLabel][_okBt(44)]|"]];
    [titleView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_sexLabel) format:@"V:|-5-[_sexLabel(30)]"]];
    [titleView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_okBt) format:@"V:|[_okBt(44)]"]];
    [titleView addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_backBt) format:@"V:|[_backBt(44)]"]];
}
-(void)ok{
    [self dismissViewControllerAnimated:NO completion:nil];
}
-(void)Setkeyboard{
    CATransition *animation = [CATransition animation];
    animation.type = kCATransitionFade;
    animation.duration = 0.2;
    __weak TranslucentViewController*tVC=self;
    NSString*V=[NSString  stringWithFormat:@"V:[_sexView(190)]|"];
    [tVC changeConstraints:V];
    
    [tVC.sexView.layer addAnimation:animation forKey:nil];
}
-(void)SetkeyboardBlock{
    CATransition *animation = [CATransition animation];
    animation.type = kCATransitionFade;
    animation.duration = 0.2;
    __weak TranslucentViewController*tVC=self;
    CGFloat aa=250;
    NSString*V=[NSString  stringWithFormat:@"V:[_sexView(190)]-(%f)-|",aa];
    [tVC changeConstraints:V];
    [tVC.sexView.layer addAnimation:animation forKey:nil];
}
/**
 *  键盘出现或者消失时，界面垂直方向约束需要重新布局；
 *
 */
-(void)changeConstraints:(NSString*)V{
    if (viewAllConstraintsOfVertical!=nil)
    {
        [self.view removeConstraints:viewAllConstraintsOfVertical];
    }
    viewAllConstraintsOfVertical=[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(_sexView) format:V];
    [self.view addConstraints:viewAllConstraintsOfVertical];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
